import Cookies from 'js-cookie'
import {beautifierConf} from "@/utils";

const state = {
  sidebar: {
    opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
    withoutAnimation: false,
    hide: false
  },
  device: 'desktop',
  size: Cookies.get('size') || 'medium',
  // 星级提醒消息
  msgdata: undefined,
  // 下推消息
  pushdata: undefined,
  // 通知消息
  noticedata: undefined,
  // 备忘录消息
  memoRecorddata: undefined,
  // 点评消息
  commnetRecorddata: undefined,
  // 分配消息提醒
  disMessage: undefined,
  // 分配给管理员 channel_push_dis_admin
  channelPushDisAdmin: undefined,
  // 分配给y业务员  channel_push_dis_bus
  channelPushDisBus: undefined,
  websocket: null,
}

const mutations = {
  SET_WEBSOCKET: (state, websocket) => {
    state.websocket = websocket;
  },
  SET_MSG_DATA: (state, {type, payload}) => {
    /* 星级提醒 */
    if (type == 'message_star') {
      state.msgdata = payload;
      /* 下推提醒  */
    } else if (type == 'message_push') {
      state.pushdata = payload;
      /* 公告消息  */
    } else if (type == 'message_notice') {
      state.noticedata = payload;
    } else if (type == 'message_memo_record') {
      state.memoRecorddata = payload;
    } else if (type == 'message_comment_record') {
      state.commnetRecorddata = payload;
    } else if (type == 'dis_message') {
      state.disMessage = payload;
    } else if (type == 'channel_push_dis_admin') {
      state.channelPushDisAdmin = payload;
    } else if (type == 'channel_push_dis_bus') {
      state.channelPushDisBus = payload;
    }
  },
  TOGGLE_SIDEBAR: state => {
    if (state.sidebar.hide) {
      return false;
    }
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
    if (state.sidebar.opened) {
      Cookies.set('sidebarStatus', 1)
    } else {
      Cookies.set('sidebarStatus', 0)
    }
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set('sidebarStatus', 0)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  },
  SET_SIZE: (state, size) => {
    state.size = size
    Cookies.set('size', size)
  },
  SET_SIDEBAR_HIDE: (state, status) => {
    state.sidebar.hide = status
  }
}

const actions = {
  setWebsocket({ commit }, websocket) {
    commit('SET_WEBSOCKET', websocket);
  },
  toggleSideBar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  },
  setSize({ commit }, size) {
    commit('SET_SIZE', size)
  },
  toggleSideBarHide({ commit }, status) {
    commit('SET_SIDEBAR_HIDE', status)
  },
  dataPush ({commit}, { type, payload }) {
    commit('SET_MSG_DATA', {type, payload})
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
